import sqlite3
import requests
import pandas as pd

class DataProcessor():
    def __init__(self,authorization: str = None):
        self.authorization = authorization

    def _generate_head(self):
        pass

    def get_data_to_database(self):
        if self.authorization == None:
            cookies = {
                'rememberMe': '0RNFcqT8dC233u8zakjQARW1Y56ILyxEDOWhKLavgdeDwfiaiA5P0fvKqH2Xw0FcYpSz2FXGfWcBrVj1C+us6M2FDDXlM4ZZrWyKaYQn6JX7y/JOJMy835G+GFdD6jjKQxpcXPQIXJfF2UedUNdSgPJpBAcXImJ5yConGJFyvb1juzkw2CO+1giOpkbfrlCNAOS50BilhGy7FFr8gQblQ04DpBRsfTR7/sUMKK6gn2Yo4Zrqi/KLrzs10LpMLjIRwES1IXZI/fscawblrG34TOQoI7oeMPedDbEsTqKWIn8obHidg90BiG6O174lmodVVY3Wp+azibkRBPBBUaFBU9sBIdFVloR/duQdCRx0GMelpNFLjZHzVckOjWjIWFwEk14rPWw+OpIWXESZfF9WUeN/OBFoVOefoGfNce8VLfJn1vpAV4jFB8ZWHUAi67Aul7vZXzJA7fljtUkzZNmiKp3fpVDyrlVtZRXn3om5msI01dhUnhxuPKXXVgRGt0k5BNfne24ajZa5Fbxq82MEDksZqbTfHO9wh8xgijdr2IZd4i4B3dftTxJwuSIdm+BEHrnSdUU6/IQAumae/XO0t+ZvIVoy53fVT5wrhL/Maxy7zdixVkzLus7gpfmGolKPjt4q/Yj9hfMQCuWfE9/aa1NzN1pGiSRemmaJTTvJqcTfxeIkK4M92wHpqxy3HCTp0HGLTOVPYD6tm/t2eRpFLwueBQs0G/mIbi7qEyGHMkjKKCa2uZNeoIJP28BWZ8xxqzLNGIPm9XH1g0ma6Cy1H9ghAmDaXrkoCIIup55IxsFsBO1YSCs0jVEPUQeFcHeTQ1H22zO4S7SfFnX7CQU05oKyqyCTBU8wauonpO3P8OSIC10+AVvUDgjXaNfJmcwX7D1mz9wE5XkcpsBoQSIF9o5jG3Syn+XontICPdrPbtfGuIrEvjfmFpb3Kz4LTyuIJr03o7ZGTw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UfTUcbSqbhw/B/2xs561udcrtIK7fnwJsCuZ0Aai5xVXQ4TX5CZnUS8tXJ7rztHmQEZWPh/ll6M7X7pa6YPBxp8dCDqo2gTAekL6bPU7dFYiCwisNasdGUCKfH/aP6mOgrXEP/kNevf8Mbq14UgPxj79Ot3Tpquvm9U6VthT7M7M63sJRWlRCPZsadWugo8nsWD5i4vCwgS1iMiFG8IqiVNOW9MRZzx0P4ov63RW7UDJ2hkjb4ZcGijmbU4rWZndsmEH04gRgx6iVwH+dt3A/Yz2tHpTYwDhXKu111jD4p/SJ2OEx07n90AlwP24XZMBKDj7WLfDUE7YLkG9M9H/ba2OBkKjfPBDXZo4eSUViFbGQhrqABsHmcsIMmg7q2CDwTsItueOxVjfWv4DQ1zPKLF/x1+bGEKpLLNBxZmL3VJic33brdge/fIImnvdZD6iMPenX/tMQE1PjCJ4E/PzjR9SLf4M4rZpvwk+isz/gJfjQt58bmKEG1m45f5lCIYErnOBuyWv/eB1V6La4MyLOJHg8+2e2ToXugKMldSNEKa2WIYD2D6oDlZI1UGOWLWn2z3FS3Av4dIcInwCB4bcoUoKENcbVjt01/tMgZ76Xyre/7kxVr6vHvAXaGf4G/JX5ZqgKzvRaXfIObXnnADtNiF9tW5j4ZMuGr9FH8E0hMJiNc5YAS+',
            }
        else:
            cookies = {
                'rememberMe': self.authorization
            }
        data = {
            'pageSize': '500',
            'pageNum': '1',
            'isAsc': 'asc',
            'feedbackType': 'all',
            'issueType': '-1',
        }
        url = 'https://iot-admin.zuoyebang.cc/iot-admin-server/system/penfeedback/list'

        all_data = []
        page_num = 1

        while True:
            data['pageNum'] = str(page_num)
            try:
                response = requests.post(
                    url=url,
                    cookies=cookies,
                    #headers=headers,
                    data=data,
                )
                if response == None:
                    break
                response.raise_for_status()
                response_json = response.json()
                rows = response_json.get('rows')
                if not rows:
                    break
                all_data.extend(rows)
                page_num += 1
                if page_num == 2:
                    break
            except requests.RequestException as e:
                print(f"请求出错: {e}")
                break

        df = pd.DataFrame(all_data)
        # df.set_index("id")

        df['createdAt'] = pd.to_datetime(df['createdAt'] / 1000, unit='s')
        # 提取年月日
        df['createdAt'] = df['createdAt'].dt.strftime('%Y-%m-%d')

        df['updatedAt'] = pd.to_datetime(df['updatedAt'] / 1000, unit='s')
        # 提取年月日
        df['updatedAt'] = df['updatedAt'].dt.strftime('%Y-%m-%d')
        conn = sqlite3.connect('data.db')
        df.to_sql("tablex",  conn, if_exists="replace", index=False)

if __name__ == "__main__":
    DataProcessor().get_data_to_database()